<?php
/**
 * Created by PhpStorm.
 * User: moquan
 * Date: 2018/6/20
 * Time: 18:20
 */

namespace app\admin\validate;

use app\lib\exception\ParamException;
use think\Exception;
use think\Request;
use think\Response;
use think\Validate;

class BaseValidate extends Validate {
    function goCheck( $scene = '' ){
        $request = Request::instance();
        $param = $request->param();
        if( $scene ){
            $this->scene( $scene );
        }
        $result = $this->batch()->check( $param );
        if( !$result ){
            throw new ParamException([
                'msg' => $this->error
            ]);
//            return view( "/public/alert", array( 'error' => $this->error ) );
        }else{
            return $param;
        }
    }

    protected function isMustInteger($value, $rule='', $data='', $field=''){
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        }
        return $field . '必须是正整数';
    }

    public function getDataByRule( $arrays ){
        if (array_key_exists('user_id', $arrays) |
            array_key_exists('uid', $arrays)) {
            // 不允许包含user_id或者uid，防止恶意覆盖user_id外键
            throw new ParamException([
                'msg' => '参数中包含有非法的参数名user_id或者uid'
            ]);
        }
        $newArray = [];
        foreach ($this->rule as $key => $value) {
            $newArray[$key] = $arrays[$key];
        }
        return $newArray;
    }
}